 package com.example.liaotian.Adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.recyclerview.widget.RecyclerView;

import com.example.liaotian.Bean.Post;
import com.example.liaotian.Fragment.ReciveFragment;
import com.example.liaotian.R;
import com.example.liaotian.activity.ReciveActivity;

import java.util.List;

/**
 * recyclerview的适配器
 */
public class    RecycAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context;
    private List<Post> data;
    private final int N_TYPE = 0;
    private final int F_TYPE = 1;
    private  int MAX_NUM = 12;//最大显示数量
    private Boolean isFootView = true;//是否为底部加载view

    public RecycAdapter(Context context, List<Post> data) {
        this.context = context;
        this.data = data;
    }

    /**
     *
     * @param parent
     * @param viewType
     * @return
     */
    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.itemo,parent,false);
        View footview = LayoutInflater.from(parent.getContext()).inflate(R.layout.foot_item,parent,false);

        if (viewType == F_TYPE){
            return new RecyclerViewHolder(footview,F_TYPE);
        }else {
            return new RecyclerViewHolder(view,N_TYPE);

        }

    }

    /**
     *
     * @param holder
     * @param position
     */
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {

        if (isFootView && (getItemViewType(position)) == F_TYPE){
            final RecyclerViewHolder recyclerViewHolder = (RecyclerViewHolder) holder;
            recyclerViewHolder.loding.setText("加载中........");
            Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    MAX_NUM += 8;
                    notifyDataSetChanged();
                }
            },1000);

        }else {
            final RecyclerViewHolder recyclerViewHolder = (RecyclerViewHolder) holder;
            Post post = data.get(position);
            recyclerViewHolder.nickname.setText(post.getNikename());
            recyclerViewHolder.title.setText(post.getTitle());
            recyclerViewHolder.time.setText(post.getCreatedAt());


            recyclerViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int position = recyclerViewHolder.getAdapterPosition();
                    Intent intent = new Intent(context, ReciveActivity.class);
                    intent.putExtra("nikename",data.get(position).getNikename());
                    intent.putExtra("time",data.get(position).getCreatedAt());
                    intent.putExtra("content",data.get(position).getContent());
                    intent.putExtra("title",data.get(position).getTitle());
                    intent.putExtra("id",data.get(position).getObjectId());
                    context.startActivity(intent);


                }
            });

        }
    }

    /**
     * 如果当前位置是显示最大值-1，那么就代表当前就是最后一个可显示的位置
     * 就代表当前应该显示foot
     * 如果不是就正常显示
     * @param position
     * @return
     */
    @Override
    public int getItemViewType(int position) {
        if (position == MAX_NUM-1){
            return F_TYPE;
        }else {
            return N_TYPE;
        }
    }

    /**
     * 如果当前的数据长度小于可显示的最大值，那么长度就为当前集合的长度
     * 如果当前数据的长度大于最大值，那么就显示最大值的数量
     * @return
     */
    @Override
    public int getItemCount() {
        if (data.size() < MAX_NUM){
            return data.size();
        }else {
            return MAX_NUM;
        }
    }

    private class RecyclerViewHolder extends RecyclerView.ViewHolder {

        public TextView nickname,title,time,loding;

        public RecyclerViewHolder(View Itemview, int viewType) {
            super(Itemview);
            if (viewType == N_TYPE) {
                nickname = Itemview.findViewById(R.id.nike);
                title = Itemview.findViewById(R.id.neirong);
                time = Itemview.findViewById(R.id.day);
            }else if (viewType == F_TYPE) {
                loding = Itemview.findViewById(R.id.foottext);
            }
        }
    }

}
